﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Flurl;
using Flurl.Http;

namespace CSharpTest
{
    public class WebResult
    {
        public bool Success { get; set; }
        public string ErrMessage { get; set; }
        public string ResponseContent { get; set; }
        public object Obj { get; set; }

        public int StatusCode { get; set; }
    }
    class Login
    {
        public string Name { get; set; }
        public string Pwd { get; set; }
    }
    class Login2
    {
        public string Name { get; set; }
        public string Pwd { get; set; }
        public string Pwd2 { get; set; }
    }
    public class FlurlApiService
    {
        /// <summary>
        /// Get请求，以对象形式传参,返回string结果
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <param name="path"></param>
        /// <param name="param"></param>
        /// <param name="millisecond">超时时间</param>
        /// <returns></returns>
        public async Task<WebResult> GetStringAsync(string ip, int port, string path, object param, int millisecond = 1000)
        {
            WebResult webResult = new WebResult();
            webResult.Success = false;

            try
            {
                var url = $"http://{ip}:{port}{path}".AppendQueryParam(param);
                var response = await url.WithTimeout(TimeSpan.FromMilliseconds(millisecond)).GetStringAsync();

                webResult.Success = true;
                webResult.ResponseContent = response;
            }
            catch (FlurlHttpTimeoutException ex)
            {
                webResult.ErrMessage = "请求超时：" + ex.Message;
            }
            catch (Exception ex)
            {
                webResult.ErrMessage = ex.Message;
            }
            return webResult;
        }

        /// <summary>
        /// Get请求，以对象形式传参，Json格式-返回object对象
        /// </summary>
        /// <typeparam name="T">返回的对象</typeparam>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <param name="path"></param>
        /// <param name="param"></param>
        /// <param name="millisecond">超时时间</param>
        /// <returns></returns>
        public async Task<WebResult> GetJsonAsync<T>(string ip, int port, string path, T param, int millisecond = 1000)
        {
            WebResult webResult = new WebResult();
            webResult.Success = false;

            try
            {
                var url = $"http://{ip}:{port}{path}".AppendQueryParam(param);

                var response = await url.WithTimeout(TimeSpan.FromMilliseconds(millisecond)).GetJsonAsync<Login>();  // 返回Login对象

                webResult.Success = true;
                webResult.Obj = response;
            }
            catch (FlurlHttpTimeoutException ex)
            {
                webResult.ErrMessage = "请求超时：" + ex.Message;
            }
            catch(Exception ex)
            {
                webResult.ErrMessage = ex.Message;
            }

            return webResult;
        }

        /// <summary>
        /// Post请求，以对象形式传参，获取T对象
        /// </summary>
        /// <typeparam name="T">返回的对象</typeparam>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <param name="path"></param>
        /// <param name="data"></param>
        /// <param name="millisecond"></param>
        /// <returns></returns>
        public async Task<WebResult> PostJsonAsync<T>(string ip, int port, string path, object data, int millisecond = 1000)
        {
            WebResult webResult = new WebResult();
            webResult.Success = false;

            try
            {
                var url = $"http://{ip}:{port}{path}";

                var response = await url.WithTimeout(TimeSpan.FromMilliseconds(millisecond)).PostJsonAsync(data).ReceiveJson<T>();  

                webResult.Success = true;
                webResult.Obj = response;
            }
            catch (FlurlHttpTimeoutException ex)
            {
                webResult.ErrMessage = "请求超时：" + ex.Message;
            }
            catch (Exception ex)
            {
                webResult.ErrMessage = ex.Message;
            }


            return webResult;
        }

        /// <summary>
        /// 返回字符串内容
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <param name="path"></param>
        /// <param name="data"></param>
        /// <param name="millisecond"></param>
        /// <returns></returns>
        public async Task<WebResult> PostJsonToStringAsync(string ip, int port, string path, object data, int millisecond = 1000)
        {
            WebResult webResult = new WebResult();
            webResult.Success = false;

            try
            {
                var url = $"http://{ip}:{port}{path}";

                var response = await url.WithTimeout(TimeSpan.FromMilliseconds(millisecond)).PostJsonAsync(data);

                webResult.Success = true;
                webResult.ResponseContent = await response.ResponseMessage.Content.ReadAsStringAsync();
                webResult.StatusCode = response.StatusCode;
            }
            catch (FlurlHttpTimeoutException ex)
            {
                webResult.ErrMessage = "请求超时：" + ex.Message;
            }
            catch (Exception ex)
            {
                webResult.ErrMessage = ex.Message;
            }


            return webResult;
        }

    }
}
